//
//  DatePickerView.m
//  cafe
//
//  Created by 尹桥印 on 15/5/29.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "DatePickerView.h"

@implementation DatePickerView

+(instancetype)datepicker{
    DatePickerView *view = [[[NSBundle mainBundle]loadNibNamed:@"DatePickerView" owner:nil options:nil] lastObject];
    return view;
}

-(void)awakeFromNib{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self addGestureRecognizer:tap];
    [self.datePicker setMinimumDate:[NSDate date]];
    [self.datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self datePickerValueChanged:self.datePicker];
}
- (void)tap:(id)sender {
    [self removeFromSuperview];
}

-(void)datePickerValueChanged:(UIDatePicker *)datePicker{
    if ([_delegate respondsToSelector:@selector(datePickerValueChange:resultStr:)]) {
        
        NSDate *date = datePicker.date;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        
        if (self.resultType == DatePickeResultType1) {
            formatter.dateFormat = @"yyyy-MM-dd HH:mm";
        }else{
            formatter.dateFormat = @"yyyy-MM-dd";
        }
        NSString *result = [formatter stringFromDate:date];
        [_delegate datePickerValueChange:self resultStr:result];
    }
}

@end
